﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xCommon.Typelib;
using Yule8xModel;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Income
{
    public partial class DailyHistory : System.Web.UI.Page
    {
        WxUserInfoResult wxUser;
        protected double totalamount; // 今日收入 / 累计收入
        protected System.Data.DataTable dtList;
        protected string title;
        protected int user_id;
        protected void Page_Load(object sender, EventArgs e)
        {
            user_id = RequestUtil.GetInteger("user_id");
            if (RuntimeDataUtil.CheckDebugModel() && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo();
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("agent"), true);
                }
            }
            /*
            object loginData = Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                Response.Redirect(WechatUrls.GetUserInfoUrl("income"), true);
            }
            */

            string date = RequestUtil.GetString("date"), sqlstmt, where=string.Empty;
            if ("today" == date)
            {
                title = "今日";
                // 今日收入：查询用户表today_income_amount字段（查询收入表sum(金额)，create_date=今天）
                sqlstmt = string.Format("select income_amount_today from t_user_finance_statistics where user_id={0}", wxUser.user_id);
                totalamount = double.Parse(DbFactory.Init(AppInfo.conn_yule8x_agent, true).GetValue(sqlstmt).ToString());
                where = string.Format(" and create_date='{0}'",DateTime.Today.ToString("yyyy-MM-dd"));
            }
            else
            {
                title = "累计";
                // 累计收入
                sqlstmt = string.Format("select income_amount_total from t_user_finance_statistics where user_id={0}", wxUser.user_id);
                totalamount = double.Parse(DbFactory.Instance.GetValue(sqlstmt).ToString());
            }
            // 交易类型(1:充值,2:返现;3:提现;4:赠送;5:领取;6:转换)
            sqlstmt = string.Format("select create_time, amount, remark from t_finance_transaction_details where user_id={0} and trade_type=2{1} order by id desc", wxUser.user_id,where);
            dtList = DbFactory.Instance.ExecuteDataTable(sqlstmt);
        }
        protected string ShortText(object ovalue,int length)
        {
            if (DBNull.Value == ovalue)
            {
                return string.Empty;
            }
            string str = ovalue.ToString();
            if (str.Length > length)
            {
                string[] arr = str.Split(new char[] { ']' }, 2);
                if (arr.Length > 1)
                {
                    length = length - arr[1].Length;
                    if (length > 0)
                    {
                        str = arr[0].Substring(0, length) + "]" + arr[1];
                    }
                }
            }
            return str;
        }
    }
}